package com.blogspot.wanyancan.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class PnTest extends Composite{
	// A Grid object; actually, an HTML table
	final private Grid grid = new Grid(5, 3);
	// Other user interface objects
	private Label nmLab = new Label();
	private Label ageLab = new Label();
	private Label homeLab = new Label();
	private TextBox nmTxt = new TextBox();
	private TextBox ageTxt = new TextBox();
	private TextBox homeTxt = new TextBox();
	private Button okBut = new Button();

	private TextArea tarea = new TextArea();
	
	// The id pf the HTMl div element that provides
	// status info
	private String statusId = "status";

	public PnTest() {
//		 set up labels and text fields
		nmLab.setText("Full Name:");
		nmTxt.setMaxLength(25);
		ageLab.setText("Age:");
		ageTxt.setVisibleLength(3);
		ageTxt.setMaxLength(3);
		homeLab.setText("Home country:");
		homeTxt.setMaxLength(25);
		

		// Place these widgets within the Grid
		grid.setWidget(0, 0, nmLab);
		grid.setWidget(0, 1, nmTxt);
		grid.setWidget(1, 0, ageLab);
		grid.setWidget(1, 1, ageTxt);
		grid.setWidget(2, 0, homeLab);
		grid.setWidget(2, 1, homeTxt);
		
		// set up button and textarea
		tarea.setCharacterWidth(40);
		tarea.setVisibleLines(25);
		okBut.setText("OK, Submit");

		// Now add these widgets to the grid
		grid.setWidget(3, 0, okBut);
		grid.setWidget(3, 1, tarea);
		// set the vertical alignment for the OK button's cell
		grid.getCellFormatter().setVerticalAlignment(3, 0,
				HasVerticalAlignment.ALIGN_TOP);
		// set the vertical alignment for the TextBoxes,
		// in order to line them up properly
		grid.getCellFormatter().setVerticalAlignment(0, 1,
				HasVerticalAlignment.ALIGN_BOTTOM);
		grid.getCellFormatter().setVerticalAlignment(1, 1,
				HasVerticalAlignment.ALIGN_BOTTOM);
		grid.getCellFormatter().setVerticalAlignment(2, 1,
				HasVerticalAlignment.ALIGN_BOTTOM);
		
		// Give the button its behavior by adding a listener object;
		// literally, a ClickListener object that has an
		// onClick event handler.
		okBut.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				// Notify the user of the remote
				// procedure call status; see method below
				showRpcStatus(true);
				// Create an instance of the client-side stub for our
				// server-side service.
				ShowRespServiceAsync respService = (ShowRespServiceAsync) GWT
						.create(ShowRespService.class);
				ServiceDefTarget endpoint = (ServiceDefTarget) respService;
				// The implementation of our service is an instance
				// of RemoteServiceServlet, so provide the server path
				// to the servlet; this path appears in web.xml
				endpoint.setServiceEntryPoint(GWT.getModuleBaseURL()+"/showresp");
				// This interface handles the response from the server.
				// It will display the server's response in a TextArea.
				// The display will be in a red color if the message
				// represents an error.
				AsyncCallback callback = new AsyncCallback() {
					public void onSuccess(Object result) {
						// If there is one, remove the 'warning' CSS style
						// relating to any error text's appearance
						if (tarea.getStyleName().equalsIgnoreCase("warning")) {
							tarea.removeStyleName("warning");
						}
						// Textarea displays the server's return value
						tarea.setText((String) result);
					}

					public void onFailure(Throwable caught) {
						// Textarea displays any exception messages
						tarea.setStyleName("warning");
						tarea
								.setText("Server request raised an error; Java exception : "
										+ caught == null ? "An unknown exception"
										: caught.getMessage());
					}
				};
				// Call the service method, validating the form
				// values first.
				try {
					respService.displayResponse(
							getPanelTextContent(grid, true) + "\""+GWT.getModuleBaseURL()+"\"", callback);
				} catch (Exception e) {
					tarea.setStyleName("warning");
					tarea.setText("Server request raised an error: "
							+ e.getMessage());
				} finally {
					// Remove the status message when we are finished
					// making the RPC call
					showRpcStatus(false);
				}
			}
		});
		initPart2();
		initWidget(grid);
	}
	private void initBloggerImageUp(){
		
	}
	// Test GetTimeService 
	private void initPart2(){
		final Button button = new Button("Click me");
		final Label label = new Label();
		button.addClickListener(new ClickListener(){
			public void onClick(Widget sender) {
				GetTimeServiceAsync serv = (GetTimeServiceAsync) GWT
					.create(GetTimeService.class);
				ServiceDefTarget tar = (ServiceDefTarget)serv;
				tar.setServiceEntryPoint(GWT.getModuleBaseURL()+"/timeService");
				serv.getTime(new AsyncCallback(){
					public void onSuccess(Object result) {
						label.setText((String)result);
					}
					public void onFailure(Throwable caught) {
						label.setText("failed:"+caught.getLocalizedMessage());
					}
				});
			}
		});
		grid.setWidget(4,0, button);
		grid.setWidget(4,1, label);
	}
	private String getPanelTextContent(HasWidgets panelType,
			boolean validateContent) {
		StringBuffer buf = new StringBuffer("");
//		String tmp = null;
		if (panelType != null) {
			// for the sake of brevity, clipped...
		}
		buf.append("not yet implemented");
		// return the TextBoxes content each
		// separated by a space
		return buf.toString();
	}
	
	protected void showRpcStatus(boolean _on) {
		// Use the GWT DOM API for JavaScript DOM
		// programming
		Element el = DOM.getElementById(statusId);
		if (el != null) {
			if (_on) {
				DOM.setStyleAttribute(el, "font-size", "1.2em");
				DOM.setStyleAttribute(el, "color", "green");
				DOM.setInnerHTML(el, "Fetching server info...");
			} else {
				DOM.setInnerHTML(el, "");
			}
		}
	}
}
